package com.nantian.logic.controller.mainControl.passwordMngControl;

import com.cuit.springboot.gdao.user.UserMapper;
import com.nantian.logic.entity.UserEntity;
import com.nantian.logic.utils.UniversalResult;
import com.nantian.model.security.User;
import com.nantian.security.repository.UserRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value="/password")
public class PasswordMngController {

    @Autowired UserRepository userRepository;
    @Autowired
    UserMapper userMapper;
    private final Log logger = LogFactory.getLog(this.getClass());
    private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

    @RequestMapping(value="/update")
    public UniversalResult updatePassword(@RequestBody Map map){
        User user=userRepository.findByUserid(map.get("userid").toString());
        System.out.println(map);

        if(bCryptPasswordEncoder.matches(map.get("password").toString(),user.getPassword())){
            map.put("passwordnew",bCryptPasswordEncoder.encode(map.get("newpassword").toString()));
            int ret=userMapper.updatePasswordByUserid(map);
            if(ret!=0)
                return new UniversalResult(UniversalResult.SUCCESS,"更改密码成功");
            else
                return new UniversalResult(UniversalResult.FAILURE,"更改密码失败");
        }else{
            return new UniversalResult(UniversalResult.FAILURE,"旧密码错误");
        }

    }
}
